package com.example.demo.service.process;

import com.example.demo.commom.CourseVoSwap;
import com.example.demo.dto.process.AddCourseDto;
import com.example.demo.entity.process.Course;
import com.example.demo.entity.process.CourseCode;
import com.example.demo.entity.user.ClassInfo;
import com.example.demo.entity.user.Student;
import com.example.demo.entity.user.Teacher;
import com.example.demo.mapper.process.CourseCodeMapper;
import com.example.demo.mapper.process.CourseMapper;
import com.example.demo.mapper.user.ClassMapper;
import com.example.demo.mapper.user.StudentMapper;
import com.example.demo.mapper.user.TeacherMapper;
import com.example.demo.service.user.TeacherService;
import com.example.demo.vo.process.CourseVo;
import com.example.demo.vo.user.TeacherInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:龙宝
 * Date:2023-03-31
 * Time:18:37
 */
@Service
public class CourseService {
    @Autowired
    private CourseCodeMapper courseCodeMapper;
    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private TeacherMapper teacherMapper;
    @Autowired
    private ClassMapper classMapper;

    // 新增课程
    public Integer add(AddCourseDto addCourseDto){
        // 查询课程是否已经存在过
        CourseCode courseCode = courseCodeMapper.getCourseCodeByCourseName(addCourseDto.getCourse_name());
        if(courseCode == null){
            // 新增一个课程code表
            Integer course_code_id = courseCodeMapper.add(addCourseDto);
            if(course_code_id == 0){
                return 0;
            }
            addCourseDto.setCourse_code_id(course_code_id);
        } else {
            addCourseDto.setCourse_code_id(courseCode.getCourse_code_id());
        }
        return courseMapper.add(addCourseDto);
    }

    // 查询所有课程信
    public List<CourseVo> getCourseAll(){
        List<CourseVo> courseVos = new ArrayList<>();
        List<Course> res = courseMapper.getCourseAll();
        // 填充教师、班级信息
        for(Course course : res){
            CourseVo courseVo = new CourseVo();
            courseVo.setCourse_id(course.getCourse_id());
            courseVo.setCourse_code_id(course.getCourse_code_id());
            courseVo.setCourse_name(course.getCourse_name());
            courseVo.setSemester(course.getSemester());
            // 教师信息
            Teacher teacher = teacherMapper.getTeacherById(course.getTeacher_id());
            TeacherInfoVo teacherInfoVo = new TeacherInfoVo();
            teacherInfoVo.setTeacher_id(teacher.getTeacher_id());
            teacherInfoVo.setUsername(teacher.getUsername());
            courseVo.setTeacherInfoVo(teacherInfoVo);
            // 班级信息
            ClassInfo classInfo = classMapper.getClassInfoByClassId(course.getClass_id());
            courseVo.setClassInfo(classInfo);
            courseVos.add(courseVo);
        }
        return courseVos;
    }
    // 查看指定班级课程信息
    public List<CourseVo> getCourseByClassId(Integer class_id){
        List<CourseVo> courseVos = new ArrayList<>();
        List<Course> res = courseMapper.getCourseByClassId(class_id);
        // 填充教师、班级信息
        for(Course course : res){
            CourseVo courseVo = new CourseVo();
            courseVo.setCourse_id(course.getCourse_id());
            courseVo.setCourse_code_id(course.getCourse_code_id());
            courseVo.setCourse_name(course.getCourse_name());
            courseVo.setSemester(course.getSemester());
            // 教师信息
            Teacher teacher = teacherMapper.getTeacherById(course.getTeacher_id());
            TeacherInfoVo teacherInfoVo = new TeacherInfoVo();
            teacherInfoVo.setTeacher_id(teacher.getTeacher_id());
            teacherInfoVo.setUsername(teacher.getUsername());
            courseVo.setTeacherInfoVo(teacherInfoVo);
            // 班级信息
            ClassInfo classInfo = classMapper.getClassInfoByClassId(course.getClass_id());
            courseVo.setClassInfo(classInfo);
            courseVos.add(courseVo);
        }
        return courseVos;
    }

    // 查看指定班级的指定学期的课程信息
    public List<CourseVo> getCourseByClassIdAndSemester(Integer class_id, Integer semester){
        List<CourseVo> courseVos = new ArrayList<>();
        List<Course> res = courseMapper.getCourseByClassIdAndSemester(class_id, semester);
        // 填充教师、班级信息
        for(Course course : res){
            CourseVo courseVo = new CourseVo();
            courseVo.setCourse_id(course.getCourse_id());
            courseVo.setCourse_code_id(course.getCourse_code_id());
            courseVo.setCourse_name(course.getCourse_name());
            courseVo.setSemester(course.getSemester());
            // 教师信息
            Teacher teacher = teacherMapper.getTeacherById(course.getTeacher_id());
            TeacherInfoVo teacherInfoVo = new TeacherInfoVo();
            teacherInfoVo.setTeacher_id(teacher.getTeacher_id());
            teacherInfoVo.setUsername(teacher.getUsername());
            courseVo.setTeacherInfoVo(teacherInfoVo);
            // 班级信息
            ClassInfo classInfo = classMapper.getClassInfoByClassId(course.getClass_id());
            courseVo.setClassInfo(classInfo);
            courseVos.add(courseVo);
        }
        return courseVos;
    }
    // 查询指定学生的所有课程
    public List<CourseVo> getCourseByStudentId(Integer student_id){
        // 查看当前学生的class_id
        Student student = studentMapper.getStudentById(student_id);
        if(student.getClass_id() == null){
            return null;
        }
        List<CourseVo> courseVos = new ArrayList<>();
        List<Course> res = courseMapper.getCourseByClassId(student.getClass_id());
        // 填充教师、班级信息
        for(Course course : res){
            CourseVo courseVo = new CourseVo();
            courseVo.setCourse_id(course.getCourse_id());
            courseVo.setCourse_code_id(course.getCourse_code_id());
            courseVo.setCourse_name(course.getCourse_name());
            courseVo.setSemester(course.getSemester());
            // 教师信息
            Teacher teacher = teacherMapper.getTeacherById(course.getTeacher_id());
            TeacherInfoVo teacherInfoVo = new TeacherInfoVo();
            teacherInfoVo.setTeacher_id(teacher.getTeacher_id());
            teacherInfoVo.setUsername(teacher.getUsername());
            courseVo.setTeacherInfoVo(teacherInfoVo);
            // 班级信息
            ClassInfo classInfo = classMapper.getClassInfoByClassId(course.getClass_id());
            courseVo.setClassInfo(classInfo);
            courseVos.add(courseVo);
        }
        return courseVos;
    }


    // 查看指定学生的指定学期的课程信息
    public List<CourseVo> getCourseByStudentIdAndSemester(Integer student_id, Integer semester){
        // 查看当前学生的class_id
        Student student = studentMapper.getStudentById(student_id);
        if(student.getClass_id() == null){
            return null;
        }
        List<CourseVo> courseVos = new ArrayList<>();
        List<Course> res = courseMapper.getCourseByClassIdAndSemester(student.getClass_id(), semester);
        // 填充教师、班级信息
        for(Course course : res){
            CourseVo courseVo = new CourseVo();
            courseVo.setCourse_id(course.getCourse_id());
            courseVo.setCourse_code_id(course.getCourse_code_id());
            courseVo.setCourse_name(course.getCourse_name());
            courseVo.setSemester(course.getSemester());
            // 教师信息
            Teacher teacher = teacherMapper.getTeacherById(course.getTeacher_id());
            TeacherInfoVo teacherInfoVo = new TeacherInfoVo();
            teacherInfoVo.setTeacher_id(teacher.getTeacher_id());
            teacherInfoVo.setUsername(teacher.getUsername());
            courseVo.setTeacherInfoVo(teacherInfoVo);
            // 班级信息
            ClassInfo classInfo = classMapper.getClassInfoByClassId(course.getClass_id());
            courseVo.setClassInfo(classInfo);
            courseVos.add(courseVo);
        }
        return courseVos;
    }

    // 查看指定id的课程
    public Course getCourseById(Integer course_id){
        return courseMapper.getCourseById(course_id);
    }
}
